from ..extensions import db
from datetime import datetime
from .user import User

class Contact(db.Model):
    __tablename__ = 'contacts'
    
    id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), primary_key=True, autoincrement=True, comment='联系人ID')
    user_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), nullable=False, comment='关联用户ID，逻辑外键 users.id')
    name = db.Column(db.String(100), nullable=False, comment='联系人姓名')
    phone = db.Column(db.String(20), nullable=False, comment='联系人电话')
    avatar = db.Column(db.String(255), comment='联系人头像')
    is_default = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), nullable=False, default=False, comment='是否默认联系人，0=否，1=是')
    relation = db.Column(db.String(50), comment='与用户关系，如父母、配偶、朋友')
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, comment='创建时间')
    
    def to_dict(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'name': self.name,
            'phone': self.phone,
            'avatar': self.avatar,
            'is_default': self.is_default,
            'relation': self.relation,
            'created_at': str(self.created_at)
        }